package com.spacewiz13.algorithm.search;

/**
 * 非递归的二分查找
 * @author David
 *
 */
public class BinSearch2 {

	public static void main(String[] args){
		int[] x = {1,3,5,6,7,8,9};
		System.out.println(search(1,x));
	}
	
	// 1356789
	public static int search(int target,int[] x){
		int low = 0;
		int high = x.length - 1;
		int mid;
		
		while(low <= high){
			mid = (low + high)/2;
			if(x[mid] == target){
				return mid;
			}else if(x[mid] > target){
				high = mid-1;
			}else{
				low = mid+1;
			}
			
		}
		
		return -1;
	}
}
